﻿namespace jzgl.Controller
{
    using jzgl.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;

    internal class DesPicDB
    {
        private SqlCommand command = null;
        private SqlConnection conn = null;
        private ConnDB conndb = new ConnDB();
        private Image image;
        private SqlDataReader reader = null;
        private DataTable table = null;

        public DesPicDB()
        {
            this.conn = this.conndb.GetConnString();
        }

        public int Add_DesPic(DesPic p)
        {
            try
            {
                try
                {
                    this.conn.Open();
                    string cmdText = "insert into DesPic(BudListKey,DesPicURL,DesPicRemark,DesPicName) values(@BudListKey ,@DesPicURL,@DesPicRemark,@name)";
                    this.command = new SqlCommand(cmdText, this.conn);
                    this.command.Parameters.Add("@BudListKey", SqlDbType.NVarChar);
                    this.command.Parameters["@BudListKey"].Value = p._BudListKey;
                    this.command.Parameters.Add("@DesPicURL", SqlDbType.Image);
                    this.command.Parameters["@DesPicURL"].Value = p.DesPicURL1;
                    this.command.Parameters.Add("@DesPicRemark", SqlDbType.NVarChar);
                    this.command.Parameters["@DesPicRemark"].Value = p._DesPicRemark;
                    this.command.Parameters.Add("@name", SqlDbType.NVarChar);
                    this.command.Parameters["@name"].Value = p._DesPicName;
                    this.command.ExecuteNonQuery();
                    this.conn.Close();
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message + "0000000000000000000000");
                }
            }
            finally
            {
            }
            return 0;
        }

        public int Count_Pic(string budgetId)
        {
            string sql = "select count(BudListKey) from DesPic where BudListKey='" + budgetId + "'";
            return this.conndb.Record_Count(sql);
        }

        public int Delete_DesPic(DesPic p)
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = "delete from DesPic where DesPicId='" + p._DesPicId + "'";
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message + "删除");
                }
            }
            finally
            {
            }
            return num;
        }

        public List<DesPic> GetPic(string key)
        {
            string strSQL = "select * from DesPic where BudListKey='" + key + "'";
            List<DesPic> list = new List<DesPic>();
            using (DataTable table = this.conndb.excuteQuery(strSQL))
            {
                foreach (DataRow row in table.Rows)
                {
                    DesPic item = new DesPic();
                    item._DesPicId = int.Parse(row["DesPicId"].ToString());
                    item._BudListKey = row["BudListKey"].ToString();
                    item._DesPicName = row["DesPicName"].ToString();
                    item._DesPicRemark = row["DesPicRemark"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public DataTable Show_DesPic()
        {
            try
            {
                try
                {
                    string sql = "select * from DesPic";
                    this.table = this.conndb.FillDataTable(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return this.table;
        }

        public Image Show_DesPic(int id)
        {
            try
            {
                this.conn.Open();
                string cmdText = "select DesPicURL from DesPic where DesPicId='" + id + "'";
                this.command = new SqlCommand(cmdText, this.conn);
                this.reader = this.command.ExecuteReader();
                if (this.reader.Read())
                {
                    byte[] buffer = (byte[]) this.reader[0];
                    MemoryStream stream = new MemoryStream(buffer);
                    this.image = Image.FromStream(stream, true);
                    stream.Close();
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                this.reader.Close();
                this.conn.Close();
            }
            return this.image;
        }

        public int UpdatePicInfo(List<DesPic> list)
        {
            int num = 0;
            for (int i = 0; i < list.Count; i++)
            {
                DesPic pic = new DesPic();
                pic = list[i];
                int num3 = list[i]._DesPicId;
                string strSQL = string.Concat(new object[] { "UPDATE DesPic SET DesPicRemark='", pic._DesPicRemark, "' WHERE DesPicId = '", num3, "'" });
                num = this.conndb.excuteUpdate(strSQL);
            }
            return num;
        }
    }
}

